﻿using System;
using System.Diagnostics.Contracts;
using System.Linq;
using Beaker.FileSystem;

namespace OpenCube.IO.Overload
{
	public static class FileSystemExtensions
	{
		/// <summary>
		/// Gets the actual parent folder of this file. Not the Overloading folder that combines them all.
		/// </summary>
		/// <param name="file">The file.</param>
		/// <returns></returns>
		public static IFolder GetActualParentFolder(this IFile file)
		{
			Contract.Requires<ArgumentNullException>(file != null);

			// when it's a fallback entry, return the actual folder instead of the combining folder
			var fallbackFile = file as OverloadingFile;
			if (fallbackFile != null)
				return fallbackFile.GetActualParentFolder();

			// not a fallback entry, do normal parent folder
			return file.ParentFolder;
		}
	}
}
